#include "stm32f10x.h"                  // Device header
#include "MyFLASH.h"

#define STORE_START_ADDRESS 		0x0800FC00
#define COUNT 						512

uint16_t Store_Data[COUNT];

void Store_Init(void)
{
	if(MyFLASH_ReadHalfWord(STORE_START_ADDRESS) != 0xA5A5)
	{
		MyFLASH_ErasePage(STORE_START_ADDRESS);
		MyFLASH_ProgramHlafWord(STORE_START_ADDRESS, 0xA5A5);
		for(uint16_t i=1;i<COUNT;i++)
		{
			MyFLASH_ProgramHlafWord(STORE_START_ADDRESS+2*i, 0x0000);
		}
	}
	
	for(uint16_t i=0;i<COUNT;i++)
	{
		Store_Data[i] = MyFLASH_ReadHalfWord(STORE_START_ADDRESS+2*i);
	}
}

void Store_Save(void)
{
	MyFLASH_ErasePage(STORE_START_ADDRESS);
	for(uint16_t i=0;i<COUNT;i++)
	{
		MyFLASH_ProgramHlafWord(STORE_START_ADDRESS+2*i, Store_Data[i]);
	}
}

void Store_Clear(void)
{
	for(uint16_t i=1;i<COUNT;i++)
	{
		Store_Data[i] = 0x0000;
	}
	Store_Save();
}
